﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
using Hardcodet.Wpf.TaskbarNotification;
using FBApp.Classes;
using System.Threading;
using Facebook;

namespace FBApp
{
	/// <summary>
	/// Interaction logic for DebugWindow.xaml
	/// </summary>
	public partial class DebugWindow : Window
	{
		
        private FacebookClient fbc;
        private Notifier notifier;		
		
		public DebugWindow()
		{
			this.InitializeComponent();
			
			// Insert code required on object creation below this point.
			notifier = new Notifier();
            fbc = new FacebookClient(notifier,webBrowser1);
		}
		
		private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            TaskbarIcon tb = (TaskbarIcon)this.FindResource("tb");
            tb.Dispose();
            fbc.Dispose();
        }
		
		
		private void btnLike_Click(object sender, RoutedEventArgs e)
        {
            notifier.displayNotification("Terrance Harris likes your status.");
        }

        private void btnComment_Click(object sender, RoutedEventArgs e)
        {
            notifier.displayNotification("Terrance Harris commented on your status.");
        }

        private void btnrelationship_Click(object sender, RoutedEventArgs e)
        {
            notifier.displayNotification("Terrance Harris is now in a relationship with some really hot brunette girl who is currently studying to be a chartered account.");            
        }

        
        private void btnWebAuthenticate_Click(object sender, RoutedEventArgs e)
        {                        
            webBrowser1.Navigated += new NavigatedEventHandler(webBrowser1_Navigated);
            webBrowser1.Navigate("https://graph.facebook.com/oauth/authorize?client_id=" + FBAuthentication.getAppID() + "&redirect_uri=http://www.facebook.com/connect/login_success.html&type=user_agent&display=popup&scope=publish_stream,read_stream,read_mailbox");
        }

        void webBrowser1_Navigated(object sender, NavigationEventArgs e)
        {
            try
            {                
                if (webBrowser1.Source != null)
                {
                    if (webBrowser1.Source.ToString().Contains("token"))
                    {
                        fbc.Authenticate(webBrowser1.Source.ToString());
                        if (fbc.IsAuthenticated())
                        {
                            webBrowser1.Visibility = Visibility.Collapsed;
                            txtresponse.Visibility = Visibility.Visible;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show("Error navigating to page: " + ex.Message);
            }
        }

        private void btnCheck_Click(object sender, RoutedEventArgs e)
        {
            if (fbc.IsAuthenticated())
            {
                MessageBox.Show("Authenticated");
            }
            else
            {
                MessageBox.Show("Not Authenticated");
            }
        }

        private void btnNotifications_Click(object sender, RoutedEventArgs e)
        {
            //List<string> results = fbc.loadNotifications(false,true);
            //for (int k=0; k < results.Count; k++)
            //{
            //    txtresponse.Text = txtresponse.Text + results.ElementAt(k) + Environment.NewLine;
            //    notifier.displayNotification(results[k]);
            //    try
            //    {
            //        Thread.Sleep(5000);
            //    }
            //    catch(Exception ex)
            //    {
            //    }                
            //}
        }

        private void btnNewsFeed_Click(object sender, RoutedEventArgs e)
        {
            //txtresponse.Text = fbc.getNewsFeed();
        }

        private void btnHomeFeed_Click(object sender, RoutedEventArgs e)
        {
            //txtresponse.Text = fbc.getHomeFeed();
        }

        private void btnRun_Click(object sender, RoutedEventArgs e)
        {
            //fbc.enableNotifications();
            btnWebAuthenticate_Click(this, e);            
        }

        private void btnError_Click(object sender, RoutedEventArgs e)
        {
            notifier.displayError("test", "EXTRA");
        }

        private void btnInbox_Click(object sender, RoutedEventArgs e)
        {
            //txtresponse.Text = fbc.getMessages(0);
        }

        private void btnOutbox_Click(object sender, RoutedEventArgs e)
        {
            //txtresponse.Text = fbc.getMessages(1);
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            //txtresponse.Text = fbc.getMessages(4);
        }

        private void btnAccessToken_Click(object sender, RoutedEventArgs e)
        {
            txtresponse.Text = fbc.getToken();
        }

        private void btnHide_Click(object sender, RoutedEventArgs e)
        {
            this.Hide();
        }

        private void btnMarkNotifications_Click(object sender, RoutedEventArgs e)
        {
            //txtresponse.Text = fbc.markAllNotificationsAsRead();
        }
	}
}